using FreeSql.DataAnnotations;

namespace SumerCoreDevOps.Domain.Entities;

/// <summary>
/// 用户操作日志
/// </summary>
[Table(Name = "user_operation_logs")]
[Index("idx_user_id_time", "UserId,CreatedAt DESC")]
[Index("idx_operation_type", "OperationType")]
[Index("idx_created_at", "CreatedAt DESC")]
public class UserOperationLog
{
    /// <summary>
    /// 日志 ID
    /// </summary>
    [Column(IsPrimary = true, IsIdentity = true)]
    public long Id { get; set; }

    /// <summary>
    /// 用户 ID
    /// </summary>
    public long? UserId { get; set; }

    /// <summary>
    /// 用户名
    /// </summary>
    [Column(StringLength = 100)]
    public string? Username { get; set; }

    /// <summary>
    /// 操作类型：
    /// - Login: 登录
    /// - Logout: 登出
    /// - CreateUser: 创建用户
    /// - UpdateUser: 更新用户
    /// - DeleteUser: 删除用户
    /// - ChangePassword: 修改密码
    /// - ResetPassword: 重置密码
    /// - LockUser: 锁定用户
    /// - UnlockUser: 解锁用户
    /// - AssignRole: 分配角色
    /// - RevokeRole: 撤销角色
    /// - CreateRole: 创建角色
    /// - UpdateRole: 更新角色
    /// - DeleteRole: 删除角色
    /// - AssignPermission: 分配权限
    /// - RevokePermission: 撤销权限
    /// </summary>
    [Column(StringLength = 50, IsNullable = false)]
    public string OperationType { get; set; } = null!;

    /// <summary>
    /// 目标对象类型（User、Role、Permission）
    /// </summary>
    [Column(StringLength = 50)]
    public string? TargetType { get; set; }

    /// <summary>
    /// 目标对象 ID
    /// </summary>
    public long? TargetId { get; set; }

    /// <summary>
    /// 目标对象名称
    /// </summary>
    [Column(StringLength = 200)]
    public string? TargetName { get; set; }

    /// <summary>
    /// 操作描述
    /// </summary>
    [Column(StringLength = 1000)]
    public string? Description { get; set; }

    /// <summary>
    /// 操作详细信息（JSON 格式）
    /// </summary>
    [Column(StringLength = -1)]
    public string? Details { get; set; }

    /// <summary>
    /// 操作状态：Success, Failed
    /// </summary>
    [Column(StringLength = 50, IsNullable = false)]
    public string Status { get; set; } = "Success";

    /// <summary>
    /// 错误消息
    /// </summary>
    [Column(StringLength = 2000)]
    public string? ErrorMessage { get; set; }

    /// <summary>
    /// IP 地址
    /// </summary>
    [Column(StringLength = 100)]
    public string? IpAddress { get; set; }

    /// <summary>
    /// 用户代理（浏览器信息）
    /// </summary>
    [Column(StringLength = 500)]
    public string? UserAgent { get; set; }

    /// <summary>
    /// 请求路径
    /// </summary>
    [Column(StringLength = 500)]
    public string? RequestPath { get; set; }

    /// <summary>
    /// 日志级别
    /// </summary>
    [Column(StringLength = 20, IsNullable = false)]
    public string LogLevel { get; set; } = "Info";

    /// <summary>
    /// 创建时间
    /// </summary>
    [Column(IsNullable = false)]
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 关联的用户
    /// </summary>
    [Navigate(nameof(UserId))]
    public User? User { get; set; }
}
